package org.jeecg.modules.lab.exam.exam.service.impl;


import org.jeecg.common.system.vo.SysDepartModel;
import org.jeecg.modules.lab.exam.entity.LabExam;
import org.jeecg.modules.lab.exam.entity.LabExamBankQuestion;
import org.jeecg.modules.lab.exam.entity.LabExamDepart;
import org.jeecg.modules.lab.exam.entity.LabExamRepo;
import org.jeecg.modules.lab.exam.exam.mapper.LabExamDepartMapper;
import org.jeecg.modules.lab.exam.exam.mapper.LabExamMapper;
import org.jeecg.modules.lab.exam.exam.mapper.LabExamRepoMapper;
import org.jeecg.modules.lab.exam.exam.service.ILabExamService;
import org.jeecg.modules.lab.exam.exam.vo.LabExamPage;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.*;

/**
 * @Description: 考试信息主表
 * @Author: jeecg-boot
 * @Date:   2023-08-26
 * @Version: V1.0
 */
@Service
public class LabExamServiceImpl extends ServiceImpl<LabExamMapper, LabExam> implements ILabExamService {

	@Autowired
	private LabExamMapper labExamMapper;
	@Autowired
	private LabExamDepartMapper labExamDepartMapper;
	@Autowired
	private LabExamRepoMapper labExamRepoMapper;

	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(LabExam labExam, List<LabExamDepart> labExamDepartList, List<LabExamRepo> labExamRepoList) {
		labExamMapper.insert(labExam);
		if(labExam.getExamDepart() !=null && labExam.getExamDepart() !=""){
			ArrayList listIds=new ArrayList(Arrays.asList(labExam.getExamDepart().split(",")));
			Iterator<String> iterator = listIds.iterator();
			while (iterator.hasNext()) {
				String dId = iterator.next();
				LabExamDepart labExamDepart= new LabExamDepart();
				labExamDepart.setExamId(labExam.getId());
				labExamDepart.setDepartId(dId);
				labExamDepartMapper.insert(labExamDepart);
			}
		}
		if(labExamRepoList!=null && labExamRepoList.size()>0) {
			for(LabExamRepo entity:labExamRepoList) {
				//外键设置
				entity.setExamId(labExam.getId());
				labExamRepoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(LabExam labExam,List<LabExamDepart> labExamDepartList,List<LabExamRepo> labExamRepoList) {
		labExam.setSynFlag("1");
		labExamMapper.updateById(labExam);
		
		//1.先删除子表数据
		labExamDepartMapper.deleteByMainId(labExam.getId());
		labExamRepoMapper.deleteByMainId(labExam.getId());
		
		//2.子表数据重新插入
//		if(labExamDepartList!=null && labExamDepartList.size()>0) {
//			for(LabExamDepart entity:labExamDepartList) {
//				//外键设置
//				entity.setExamId(labExam.getId());
//				labExamDepartMapper.insert(entity);
//			}
//		}
		if(labExam.getExamDepart() !=null && labExam.getExamDepart() !=""){
			ArrayList listIds=new ArrayList(Arrays.asList(labExam.getExamDepart().split(",")));
			Iterator<String> iterator = listIds.iterator();
			while (iterator.hasNext()) {
				String dId = iterator.next();
				LabExamDepart labExamDepart= new LabExamDepart();
				labExamDepart.setExamId(labExam.getId());
				labExamDepart.setDepartId(dId);
				labExamDepartMapper.insert(labExamDepart);
			}
		}
		if(labExamRepoList!=null && labExamRepoList.size()>0) {
			for(LabExamRepo entity:labExamRepoList) {
				//外键设置
				entity.setExamId(labExam.getId());
				labExamRepoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		labExamDepartMapper.deleteByMainId(id);
		labExamRepoMapper.deleteByMainId(id);
		labExamMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			labExamDepartMapper.deleteByMainId(id.toString());
			labExamRepoMapper.deleteByMainId(id.toString());
			labExamMapper.deleteById(id);
		}
	}

	@Override
	public List<LabExamPage> selectExamListByUserId(String userId) {
		return labExamMapper.selectExamListByUserId(userId);
	}

}
